package testIntergration;


import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import FamilyTherapy.Board;
import FamilyTherapy.GCWindow;
import FamilyTherapy.Game;
import FamilyTherapy.MagicToolCard;
import FamilyTherapy.Player;

public class testGame_checkPosition {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
	

	
	@Test
	public void testCheckPosition() throws Exception {
		int currentPlayerPos = 0;
		int currentPlayerNo = 0;
		int expectedResult = 0;
		
		

		Player[] p = { new Player("a", 1), new Player("b", 2) };
		GCWindow gcWindow = new GCWindow();
		Game game = new Game();
		Board board = new Board();
		 
		int currentEvent= board.getBox(currentPlayerPos);

		Method checkPositionMethod = game.getClass().getDeclaredMethod(
				"checkPosition", GCWindow.class, int.class, Player[].class);
		
		
		checkPositionMethod.setAccessible(true);
		checkPositionMethod.invoke(game, gcWindow,
				currentPlayerNo, p);
		
		//currentPlayerPos = p[0].getPosition();
		currentEvent = board.getBox(currentPlayerPos);
		
		checkPositionMethod.setAccessible(false);
		System.out.println(currentEvent);
		//System.out.println(p[0].getPosition());
		
		assertEquals(expectedResult, currentEvent);
	}
	


	
	@Test
	public void testCheckPosition_1() throws Exception {
		int currentPlayerPos = 4;
		int currentPlayerNo = 0;
		int expectedResult = 1;
		class GCWindowStub extends GCWindow{
			@Override
			public int requestUsingMT(int currentPlayerNo, Player p[])
			{
			// a simple implementation,
			// which is specially designed for our test case: (5, 1, 1)
			return -1;
			}
		}
		 
		Player[] p = { new Player("a", 1), new Player("b", 2) };
		GCWindow gcWindow = new GCWindowStub();
	
		p[0].setPosition(currentPlayerPos);
		Game game = new Game();
		game.setNoOfPlayer(2);
		Board board = new Board();
	
		int currentEvent= board.getBox(currentPlayerPos);
		 
		Method checkPositionMethod = game.getClass().getDeclaredMethod(
		"checkPosition", GCWindow.class, int.class, Player[].class);

		 
		checkPositionMethod.setAccessible(true);

	
		String data = "1"; //data to be input 
		InputStream stdin = System.in; 
		try { 
			System.setIn(new ByteArrayInputStream(data.getBytes()));
			checkPositionMethod.invoke(game, gcWindow,currentPlayerNo,p); 
		} 
		finally { 
			System.setIn(stdin); //reset input stream as System.in
		} 
	
		
		checkPositionMethod.setAccessible(false);
		//drawEventMethod.setAccessible(false);
	
		assertEquals(expectedResult, currentEvent);
	}

	
	@Test
	public void testCheckPosition_2() throws Exception {
		int currentPlayerPos = 2;
		int currentPlayerNo = 0;
		int expectedResult = 2;
		
		

		Player[] p = { new Player("a", 1), new Player("b", 2) };
		GCWindow gcWindow = new GCWindow();
		p[0].setPosition(currentPlayerPos);
		Game game = new Game();
		Board board = new Board();
		 
		
		int currentEvent= board.getBox(currentPlayerPos);

		Method checkPositionMethod = game.getClass().getDeclaredMethod(
				"checkPosition", GCWindow.class, int.class, Player[].class);
		
		
		checkPositionMethod.setAccessible(true);
		checkPositionMethod.invoke(game, gcWindow,
				currentPlayerNo, p);
		
		//currentPlayerPos = p[0].getPosition();
		currentEvent = board.getBox(currentPlayerPos);
		
		checkPositionMethod.setAccessible(false);
		System.out.println(currentEvent);
		//System.out.println(p[0].getPosition());
		
		assertEquals(expectedResult, currentEvent);
	}
	
	
  
}
